<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 200px;
            height: 200px;
            background-color: skyblue;
            position: absolute;
        }

        button {
            height: 50px;
        }
    </style>
</head>

<body>

    <div><button id="hui">回放</button></div>
    <script>
        var di = document.querySelector("div")
        var arrx = []
        var arry = []
        di.onmousedown = function () {

            console.log(11)

            di.onmousemove = function (e) {

                e = e || window.event

                var x = e.clientX - di.offsetWidth / 2
                var y = e.clientY - di.offsetHeight / 2
                if (x < 0) {
                    x = 0
                }
                if (y < 0) {
                    y = 0
                }
                if (x >= window.innerWidth - di.offsetWidth) {
                    x = window.innerWidth - di.offsetWidth
                }
                if (y >= window.innerHeight - di.offsetWidth) {
                    y = window.innerHeight - di.offsetHeight
                }
                arrx.unshift(x)
                arry.unshift(y)
                di.style.left = x + "px"
                di.style.top = y + "px"
            }
        }

        var hui = document.getElementById("hui")
        hui.onclick = function (e) {
            e = e || window.event
            console.log(arrx)
            var i = 0
            var inter = setInterval(function () {
                di.style.left = arrx[i] + "px"
                di.style.top = arry[i] + 'px'
                if (i == arrx.length - 1) {
                    clearInterval(inter)
                    arrx = []
                    arry = []
                }
                i++
            }, 3)


        }


        di.onmouseup = function () {
            //清除一个事件，只需要将事件的值设置null

            di.onmousemove = null;
        };
    </script>
</body>

</html>